package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.DiseaseTypeDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("DiseaseType")
public class DiseaseTypeController extends BaseController{


    @Autowired
    @Qualifier("DiseaseTypeServiceImplOne")
    private DiseaseTypeService diseaseTypeService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;

    @Autowired
    @Qualifier("InstitutionServiceImplOne")
    private InstitutionService institutionService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;



    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String name = jsonObject.getString("name");
            String staffCode = jsonObject.getString("staffCode");


            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ElderlyClass/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String insCode = institutionStaffRel.getInstitutionCode();


            DiseaseType diseaseType =new DiseaseType();
            diseaseType.setDiseaseTypeCode(UUID.randomUUID().toString());
            diseaseType.setName(name);
            diseaseType.setSort(0);
            diseaseType.setStatus(1);
            diseaseType.setInstitutionCode(insCode);
            diseaseTypeService.insert(diseaseType);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);

        } catch (Exception e) {
            insertError("/ElderlyClass/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int sort = jsonObject.getInteger("sort");
            int status = jsonObject.getInteger("status");

            String diseaseTypeCode =jsonObject.getString("diseaseTypeCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ElderlyClass/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            DiseaseType diseaseType =diseaseTypeService.getByDiseaseTypeCode(diseaseTypeCode);

            diseaseType.setName(name);
            diseaseType.setSort(sort);
            diseaseType.setStatus(status);
            diseaseTypeService.update(diseaseType);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ElderlyClass/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult getList(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ElderlyClass/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String insCode = institutionStaffRel.getInstitutionCode();

            List<DiseaseType> listElderlyClass = diseaseTypeService.listByInsCode(insCode);

            return success(listElderlyClass);
        } catch (Exception e) {
            insertError("/ElderlyClass/List",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ElderlyClass/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String insCode = institutionStaffRel.getInstitutionCode();

            PageInfo<DiseaseType> elderlyClassPageInfo =diseaseTypeService.pageByInsCode(pageIndex,pageSize,insCode);

            return success(elderlyClassPageInfo);
        } catch (Exception e) {
            insertError("/ElderlyClass/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "GovPage",method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ElderlyClass/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<DiseaseType> pageInfo =diseaseTypeService.pageByGovCode(pageIndex,pageSize,staff.getGovernmentCode());

            List<DiseaseTypeDTO> listDTO = new ArrayList<>();
            for(DiseaseType diseaseType:pageInfo.getList()){
                DiseaseTypeDTO dto = mapper.map(diseaseType,DiseaseTypeDTO.class);
                Institution institution = institutionService.getByInstitutionCode(diseaseType.getInstitutionCode());
                User user = userService.getByUserCode(institution.getUserCode());
                dto.setInstitutionName(user.getName());

                listDTO.add(dto);
            }

            PageInfo<DiseaseTypeDTO> pageInfo1 = new PageInfo<>();
            pageInfo1.setList(listDTO);
            pageInfo1.setTotal(pageInfo.getTotal());

            return success(pageInfo1);
        } catch (Exception e) {
            insertError("/ElderlyClass/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "AddForOut2", method = RequestMethod.POST)
    public MyResult AddForOut2(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String institutionCode = jsonObject.getString("institutionCode");
            JSONArray arrName = jsonObject.getJSONArray("arrName");

            if (!checkSignForOut(timeStr, sign)) {
                return failed("签名错误");
            }

            for(int i=0;i<arrName.size();i++){
                DiseaseType diseaseType =new DiseaseType();
                diseaseType.setDiseaseTypeCode(UUID.randomUUID().toString());
                diseaseType.setName(arrName.getString(i));
                diseaseType.setSort(0);
                diseaseType.setInstitutionCode(institutionCode);
                //diseaseTypeService.insert(diseaseType);
            }

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);

        } catch (Exception e) {
            insertError("/DiseaseType/AddForOut",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


}
